﻿using System.Collections.Generic;
using System.Linq;

namespace LuatChoi
{
    public class BanCo
    {
        public QuanCo[,] Ban;


        public BanCo()
        {
            Ban = new QuanCo[8, 8];

            //tạo tốt
            for (int i = 0; i < 8; i++)
            {
                Ban[i, 1] = new Tot(Phe.Trang, this, i, 1);
                Ban[i, 6] = new Tot(Phe.Den, this, i, 6);
            }
            //vua
            Ban[4, 0] = new Vua(Phe.Trang, this, 4, 0);
            Ban[3, 7] = new Vua(Phe.Den, this, 3, 7);

            //hậu
            Ban[3, 0] = new Hau(Phe.Trang, this, 3, 0);
            Ban[4, 7] = new Hau(Phe.Den, this, 4, 7);

            //xe
            Ban[0, 0] = new Xe(Phe.Trang, this, 0, 0);
            Ban[7, 0] = new Xe(Phe.Trang, this, 7, 0);

            Ban[0, 7] = new Xe(Phe.Den, this, 0, 7);
            Ban[7, 7] = new Xe(Phe.Den, this, 7, 7);

            //mã
            Ban[1, 0] = new Ma(Phe.Trang, this, 1, 0);
            Ban[6, 0] = new Ma(Phe.Trang, this, 6, 0);

            Ban[1, 7] = new Ma(Phe.Den, this, 1, 7);
            Ban[6, 7] = new Ma(Phe.Den, this, 6, 7);

            //tượng
            Ban[2, 0] = new Tuong(Phe.Trang, this, 2, 0);
            Ban[5, 0] = new Tuong(Phe.Trang, this, 5, 0);

            Ban[2, 7] = new Tuong(Phe.Den, this, 2, 7);
            Ban[5, 7] = new Tuong(Phe.Den, this, 5, 7);


            //test
            //Ban[3,4] = new Hau(Phe.Trang, this, 3, 4);
            //for (int i = 0; i < 8; i++)
            //{
            //    Ban[i, 6] = new Tot(Phe.Trang, this, i, 6);
            //}
        }

        public QuanCo this[int x, int y]
        {
            get { return Ban[x, y]; }
            set { Ban[x, y] = value; }
        }

        public QuanCo this[Vector v]
        {
            get { return Ban[v.X, v.Y]; }
            set { Ban[v.X, v.Y] = value; }
        }

        public List<Vector> BiChieu
        {
            get
            {
                var ls = new List<Vector>();
                for (int i = 0; i < 7; i++)
                    for (int j = 0; j < 7; j++)
                    {
                        var quan = Ban[i, j];
                        if (quan != null)
                        {
                            quan.TinhToaDoAnhHuong();
                            ls.AddRange(quan.ToaDoCoTheTanCong);
                        }
                    }
                return ls.Where(v => Ban[v.X, v.Y] is Vua).ToList();
            }

        }
    }
}